package com.example.assignment1.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.Timer;
import com.google.gwt.http.client.URL;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Assignment1 implements EntryPoint {
	
	private VerticalPanel mainPanel = new VerticalPanel();
	private HorizontalPanel addPanel = new HorizontalPanel();
	private TextBox addStringTextBox = new TextBox();
	private Button submitString = new Button("Submit");
	private HTML authHtml = new HTML();
	
	boolean isLoggedIn = false;
	String username = "";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	
	private final TypedStringServiceAsync stringService = GWT
			.create(TypedStringService.class);
	
	
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		//Timer for refreshing Tweets
		Timer tweetRefreshTimer = new Timer(){
			@Override
			public void run() {
				Assignment1.this.Refresh();
		    } 
		};
		tweetRefreshTimer.scheduleRepeating(5000);
		
		//User service
		stringService.getUser(new AsyncCallback<String>(){
			public void onFailure(Throwable error){
	    		
	    	}
	    	public void onSuccess(String response)
	    	{
	    		if (response.length() == 0){
	    			stringService.getLogInLink(URL.encode(Location.getPath() + "" + Location.getQueryString()), new AsyncCallback<String>(){
	    				public void onFailure(Throwable error){
	    		    		
	    		    	}
	    		    	public void onSuccess(String response)
	    		    	{
	    		    		Anchor authLink = new Anchor("Log In to Tweet", response);
	    		    		mainPanel.add(authLink);
	    		    	}
	    			});
	    			
	    			
	    		}else{
	    			isLoggedIn = true;
	    			username = response;
	    			addPanel.add(addStringTextBox);
	    			addPanel.add(submitString);
	    			stringService.getLogOutLink(URL.encode(Location.getPath() + "" + Location.getQueryString()), new AsyncCallback<String>(){
	    				public void onFailure(Throwable error){
	    		    		
	    		    	}
	    		    	public void onSuccess(String response)
	    		    	{
	    		    		Anchor authLink = new Anchor("Log Out", response);
	    		    		mainPanel.add(authLink);
	    		    	}
	    			});
	    		}
	    		
	    	}
	    });
        
		//Panels Setup
		mainPanel.add(addPanel);
		
		RootPanel.get("tweet_panel").add(mainPanel);
		
		// Move cursor focus to the input box.
	    addStringTextBox.setFocus(true);
	    
	    Refresh();
	    
	 // Listen for mouse events on the Submit button.
	    submitString.addClickHandler(new ClickHandler() {
	      public void onClick(ClickEvent event) {
	    	  String text = addStringTextBox.getText();
	    	  if(text == "")
	    		  return;
	    	  addToTweets(text);
	      }
	    });
	    
	    
	    // Listen for keyboard events in the input box.
	    addStringTextBox.addKeyPressHandler(new KeyPressHandler() {
	      public void onKeyPress(KeyPressEvent event) {
	        if (event.getCharCode() == KeyCodes.KEY_ENTER) {
	        	String text = addStringTextBox.getText();
	        	if(text == "")
	        		return;
	        	addToTweets(text);
	        }
	      }
	    });
		
	}
	
	private void Refresh(){
		stringService.getAllStrings(new AsyncCallback<String>(){
	    	public void onFailure(Throwable error){
	    		
	    	}
	    	
	    	public void onSuccess(String response)
	    	{
	    		HTML output = new HTML();
	    		output.setHTML(response);
	    		RootPanel.get("tweets").clear();
	    		RootPanel.get("tweets").add(output);
	    	}
	    });
	}
	
	private void addToTweets(String text)
	{
		if (text.trim().length()==0)
			return;
		AsyncCallback<String> write = new AsyncCallback<String>(){
			public void onFailure(Throwable error)
			{
				
			}
			public void onSuccess(String s)
			{
				HTML output = new HTML();
				output.setHTML(s);
				RootPanel.get("tweets").insert(output, 0);
				addStringTextBox.setText("");
			}
		};
		stringService.addString(text, write);
	}
	
	
}
